package com.person.jc.web.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.service.BsOrderService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.JsonMapper;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

/**
 * fcl维修接单
 */
public class BsOrderController extends Controller {
	
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		setAttr("startDate",DateUtils.formatDate(DateUtils.addMonths(new Date(), -1), "yyyy-MM-dd"));
		setAttr("endDate",DateUtils.getDate());
		String orderType = getPara("orderType");//1. 检测接单
		if("1".equals(orderType)){
			render("check/main.jsp");
		}else{
			render("repair/main.jsp");
		}
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BsOrderService.me.getList(map);
		setAttr("list", list);
		String orderType = String.valueOf(map.get("orderType"));
		if("1".equals(orderType)){
			render("check/list.jsp");
		}else{
			render("repair/list.jsp");
		}
	}
	
	public void edit(){
		String orderId = getPara("orderId");
		String taskType = getPara("taskType");
		String view = getPara("view");
		if(Utils.isNotEmpty(orderId)){
			Record order = BsOrderService.me.getOrderInfo(orderId);
			List<Record> detail = BsOrderService.me.getOrderDetail(orderId, taskType);
			setAttr("obj", order);
			setAttr("details", detail);
			if(detail.size() > 0){
				setAttr("detailsJson", JsonMapper.nonDefaultMapper().toJson(detail));
			}else{
				setAttr("detailsJson", "[]");
			}
		}else{
			setAttr("detailsJson", "[]");
		}
		setAttr("view", view);
		setAttr("taskType", taskType);
		String orderType = getPara("orderType");
		if("1".equals(orderType)){
			render("check/edit.jsp");
		}else{
			render("repair/edit.jsp");
		}
	}
	
	@Before(Tx.class)
	public void save(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String orderId = String.valueOf(map.get("orderId"));
		String orderType = String.valueOf(map.get("orderType"));
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		map.put("optUser", tsUser.getUserId());
		if(Utils.isEmpty(orderId)){
			orderId = BsOrderService.me.save(orderType, map);
		}else{
			BsOrderService.me.update(orderType, map);
		}
		BsOrderService.me.deleteDetail(orderType, orderId);
		
		String[] deviceName1 = getParaValues("deviceName1");
		String[] deviceName2 = getParaValues("deviceName2");
		String[] deviceName3 = getParaValues("deviceName3");
		
		int arrLen = 0;
		if(deviceName1!=null && deviceName1.length>1){arrLen+=deviceName1.length-1;}
		if(deviceName2!=null && deviceName2.length>1){arrLen+=deviceName2.length-1;}
		if(deviceName3!=null && deviceName3.length>1){arrLen+=deviceName3.length-1;}
		Object[][] params = null;
		int i = 0;
		if(arrLen>0){
			params = new Object[arrLen][16];
		}
		
		
		String[] deviceManufactor1 = getParaValues("deviceManufactor1");
		String[] deivceNo1 = getParaValues("deivceNo1");
		String[] volumeCategory1 = getParaValues("volumeCategory1");
		String[] manufactureDate1 = getParaValues("manufactureDate1");
		String[] packStatus1 = getParaValues("packStatus1");
		String[] lookStatus1 = getParaValues("lookStatus1");
		String[] fileStatus1 = getParaValues("fileStatus1");
		String[] issueDesc1 = getParaValues("issueDesc1");
		String[] ifCert1 = getParaValues("ifCert1");

		String[] remark1 = getParaValues("remark1");
		if(deviceName1!=null && deviceName1.length>1){
			for (; i < deviceName1.length-1; i++) {
				params[i][0] = orderId;		
				params[i][1] = "1";//device_type 气瓶
				params[i][2] = deviceName1[i];
				params[i][3] = deivceNo1[i];
				params[i][4] = null;//device_model
				params[i][5] = deviceManufactor1[i];
				params[i][6] = null;//gas_type
				params[i][7] = packStatus1[i];
				params[i][8] = lookStatus1[i];
				params[i][9] = fileStatus1[i];
				params[i][10] = (issueDesc1 != null && issueDesc1.length >= i && Utils.isNotEmpty(issueDesc1[i])) ? issueDesc1[i] : null;

				if(orderType.equals("1")){
					params[i][11]=1;
				}else{

					params[i][11] = (ifCert1 != null && ifCert1.length >= i && Utils.isNotEmpty(ifCert1[i])) ? ifCert1[i] : 0;
				}





				params[i][12] = (remark1 != null && remark1.length >= i && Utils.isNotEmpty(remark1[i])) ? remark1[i] : null; 
				params[i][13] = tsUser.getUserId();
				params[i][14] = volumeCategory1[i];
				params[i][15] = manufactureDate1[i];
			}
		}
		
		
		String[] deviceManufactor2 = getParaValues("deviceManufactor2");
		String[] deviceModel2 = getParaValues("deviceModel2");
		String[] deivceNo2 = getParaValues("deivceNo2");
		String[] packStatus2 = getParaValues("packStatus2");
		String[] lookStatus2 = getParaValues("lookStatus2");
		String[] fileStatus2 = getParaValues("fileStatus2");
		String[] issueDesc2 = getParaValues("issueDesc2");
		String[] ifCert2 = getParaValues("ifCert2");
		String[] remark2 = getParaValues("remark2");
		if(deviceName2!=null && deviceName2.length>1){
			for (int j=0; j < deviceName2.length-1; i++, j++) {
				String[] gayTypes = getParaValues("gasType2_"+(j+1));
				params[i][0] = orderId;		
				params[i][1] = "3";//device_type 气检仪
				params[i][2] = deviceName2[j];
				params[i][3] = deivceNo2[j];
				params[i][4] = deviceModel2[j];//device_model
				params[i][5] = deviceManufactor2[j];
				params[i][6] = Utils.toString(gayTypes);//gas_type gasType2_1
				params[i][7] = packStatus2[j];
				params[i][8] = lookStatus2[j];
				params[i][9] = fileStatus2[j];
				params[i][10] = (issueDesc2 != null && issueDesc2.length >= j && Utils.isNotEmpty(issueDesc2[j])) ? issueDesc2[j] : null;


				if(orderType.equals("1")){
					params[i][11]=1;
				}else {
					params[i][11] = (ifCert2 != null && ifCert2.length >= j && Utils.isNotEmpty(ifCert2[j])) ? ifCert2[j] : 0;
				}
				params[i][12] = (remark2 != null && remark2.length >= j && Utils.isNotEmpty(remark2[j])) ? remark2[j] : null;
				params[i][13] = tsUser.getUserId();
				params[i][14] = null;
				params[i][15] = null;
			}
		}
		
		
		String[] deviceManufactor3 = getParaValues("deviceManufactor3");
		String[] deviceModel3 = getParaValues("deviceModel3");
		String[] deivceNo3 = getParaValues("deivceNo3");
		String[] packStatus3 = getParaValues("packStatus3");
		String[] lookStatus3 = getParaValues("lookStatus3");
		String[] fileStatus3 = getParaValues("fileStatus3");
		String[] issueDesc3 = getParaValues("issueDesc3");
		String[] ifCert3 = getParaValues("ifCert3");
		String[] remark3 = getParaValues("remark3");
		if(deviceName3!=null && deivceNo3.length>1){
			for (int j=0; j < deviceName3.length-1; i++, j++) {
				params[i][0] = orderId;		
				params[i][1] = "2";//device_type 空呼
				params[i][2] = deviceName3[j];
				params[i][3] = deivceNo3[j];
				params[i][4] = deviceModel3[j];//device_model
				params[i][5] = deviceManufactor3[j];
				params[i][6] = null;//gas_type 
				params[i][7] = packStatus3[j];
				params[i][8] = lookStatus3[j];
				params[i][9] = fileStatus3[j];
				params[i][10] = (issueDesc3 != null && issueDesc3.length >= j && Utils.isNotEmpty(issueDesc3[j])) ? issueDesc3[j] : null;

				if(orderType.equals("1")){
					params[i][11]=1;
				}else {
					params[i][11] = (ifCert3 != null && ifCert3.length >= j && Utils.isNotEmpty(ifCert3[j])) ? ifCert3[j] : 0;
				}
				params[i][12] = (remark3 != null && remark3.length >= j && Utils.isNotEmpty(remark3[j])) ? remark3[j] : null;
				params[i][13] = tsUser.getUserId();
				params[i][14] = null;
				params[i][15] = null;
			}
		}
		
		if(params != null){
			BsOrderService.me.saveDetail(orderType, orderId, params, arrLen);
		}
		renderText("success");
	}
	
	@Before(Tx.class)
	public void delete(){
		String orderIds = getPara("orderIds");
		String orderType = getPara("orderType");
		for (String orderId : orderIds.split(",")) {
			BsOrderService.me.delete(orderType, orderId);
			BsOrderService.me.deleteDetail(orderType, orderId);
		}
		renderText("success");
	}
	
	
	public void uniqueDeivceNo(){
		String params = getPara("params");
		String orderId = getPara("orderId");
		
		Record record = BsOrderService.me.uniqueDeivceNo(orderId, params);
		String result = record.getStr("devicenos");
		if(Utils.isEmpty(result)){
			result = "success";
		}
		renderText(result);
	}
	
}
